package test.marui.com.mytest.utils;

import android.graphics.Bitmap;
import android.graphics.Matrix;
import android.support.annotation.NonNull;
import android.view.View;

/**
 * Created by MR on 2019/1/25.
 */
public class BitmapUtil {

    private BitmapUtil() {
        throw new UnsupportedOperationException("Cannot be instantiated");
    }

    public static Bitmap snapshot(View view) {
        view.setDrawingCacheEnabled(true);
        view.buildDrawingCache(true);
        view.destroyDrawingCache();
        return view.getDrawingCache();
    }

    public static Bitmap scale(@NonNull Bitmap bitmap, float newWidth, float newHeight) {
        int width = bitmap.getWidth();
        int height = bitmap.getHeight();
        Matrix matrix = new Matrix();
        if (width != newWidth || height != newHeight) {
            float scaleWidth = newWidth / (float) width;
            float scaleHeight = newHeight / (float) height;
            matrix.setScale(scaleWidth, scaleHeight);
        }
        return Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true);
    }
}
